﻿#pragma once

#include<iostream>
using namespace std;

enum Color
{
	RED,
	BLACK
};

template<class K, class V>
struct BRTreeNode
{
	pair<K, V> _kv;
	BRTreeNode<K, V>* _left;
	BRTreeNode<K, V>* _right;
	BRTreeNode<K, V>* _parent;
	Color _color;

	BRTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_color(RED)
	{}
};

template<class K, class V>
class BRTree
{
	typedef BRTreeNode<K, V> Node;
public:
	bool insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_color = BLACK;
			return true;
		}
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		cur->_color = RED;
		if (kv.first < parent->_kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_color == RED)
		{
			Node* grandparent = parent->_parent;
			if (grandparent->_left == parent)
			{
				//    g
				// p     u
				Node* uncle = grandparent->_right;
				if (uncle && uncle->_color == RED)
				{
					parent->_color = BLACK;
					uncle->_color = BLACK;
					grandparent->_color = RED;

					cur = grandparent;
					parent = cur->_parent;
				}
				else//uncle不存在或者uncle是黑色
				{
					if (cur == parent->_left)
					{
						RotateR(grandparent);
						parent->_color = BLACK;
						grandparent->_color = RED;
					}
					else//(cur == parent->_right)
					{
						RotateL(parent);
						RotateR(grandparent);
						cur->_color = BLACK;
						grandparent->_color = RED;
					}
					break;
				}
			}
			else//(grandparent->_right == parent)
			{
				Node* uncle = grandparent->_left;
				if (uncle && uncle->_color == RED)
				{
					//    g
					// u     p
					parent->_color = BLACK;
					uncle->_color = BLACK;
					grandparent->_color = RED;

					cur = grandparent;
					parent = cur->_parent;
				}
				else//uncle不存在或者uncle是黑色
				{
					if (cur == parent->_right)
					{
						RotateL(grandparent);
						parent->_color = BLACK;
						grandparent->_color = RED;
					}
					else//cur == parent->_left
					{
						RotateR(parent);
						RotateL(grandparent);
						cur->_color = BLACK;
						grandparent->_color = RED;
					}
					break;
				}
			}
		}
		_root->_color = BLACK;

		return true;
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		subL->_right = parent;

		if (subLR)
			subLR->_parent = parent;

		Node* tmp = parent->_parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			subL->_parent = nullptr;
			_root = subL;
		}
		else
		{
			if (tmp->_left == parent)
			{
				subL->_parent = tmp;
				tmp->_left = subL;
			}
			else
			{
				subL->_parent = tmp;
				tmp->_right = subL;
			}
		}
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;

		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}

	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了

			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在黑色结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}

		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了

		if (root->_color == RED && root->_parent->_color == RED)
		{
			cout << root->_kv.first << "存在连续红色节点" << endl;
			return false;
		}
		if (root->_color == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}
	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_color == RED)
			return false;
		// 参考值

		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_color == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

private:
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << " ";
		_InOrder(root->_right);
		return;
	}

private:
	Node* _root = nullptr;
};
